//
//  String + Extension.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/12/10.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

extension String {
    /// 查找链接和来源
    ///
    /// - returns:（链接， 来源）
    func href () -> (link: String, source: String)? {
        
        let pattern = "<a href=\"(.*?)\".*?>(.*?)</a>"
        let regex = try! NSRegularExpression(pattern: pattern, options: [.CaseInsensitive])
        
        guard let result = regex.firstMatchInString(self,
            options: [],
            range: NSRange(location: 0, length: self.characters.count)) else {
            QL2("没有查询到结果")
            return nil
        }
        
        let str = self as NSString
        
        let link = str.substringWithRange(result.rangeAtIndex(1))
        let source = str.substringWithRange(result.rangeAtIndex(2))
        
        return (link, source)
    }
    
}
